/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import object.TetrisTask;
import java.util.Timer;


/**
 *
 * @author XiaoLong
 */
public class MainFrame extends javax.swing.JFrame {

    
    //暂停的标识, true为暂停
    private boolean paused = false;

    // private Thread music;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        gamePanel.setNextPanel(this.nextPanel);
        this.gamePanel.showNextPanel();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rightPanel = new javax.swing.JPanel();
        nextPanel = new ui.NextPanel();
        beginBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        scoreLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        levelLabel = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        linesLabel = new javax.swing.JLabel();
        gamePanel = new ui.GamePanel(this);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("小俄罗斯");
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("GameFrame"); // NOI18N
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        rightPanel.setBackground(new java.awt.Color(0, 0, 0));
        rightPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        nextPanel.setBackground(new java.awt.Color(0, 0, 0));
        nextPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        nextPanel.setMaximumSize(new java.awt.Dimension(80, 80));
        nextPanel.setMinimumSize(new java.awt.Dimension(80, 80));
        nextPanel.setPreferredSize(new java.awt.Dimension(80, 80));

        javax.swing.GroupLayout nextPanelLayout = new javax.swing.GroupLayout(nextPanel);
        nextPanel.setLayout(nextPanelLayout);
        nextPanelLayout.setHorizontalGroup(
            nextPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 78, Short.MAX_VALUE)
        );
        nextPanelLayout.setVerticalGroup(
            nextPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 78, Short.MAX_VALUE)
        );

        beginBtn.setBackground(new java.awt.Color(51, 51, 51));
        beginBtn.setFont(new java.awt.Font("微软雅黑", 0, 14)); // NOI18N
        beginBtn.setForeground(new java.awt.Color(255, 255, 255));
        beginBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/play.png"))); // NOI18N
        beginBtn.setText("Play");
        beginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                beginBtnActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("微软雅黑", 2, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("SCORE");

        scoreLabel.setFont(new java.awt.Font("微软雅黑", 2, 14)); // NOI18N
        scoreLabel.setForeground(new java.awt.Color(0, 255, 0));
        scoreLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        scoreLabel.setText("0");
        scoreLabel.setToolTipText("");

        jLabel4.setFont(new java.awt.Font("微软雅黑", 2, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("LEVEL");

        levelLabel.setFont(new java.awt.Font("微软雅黑", 2, 14)); // NOI18N
        levelLabel.setForeground(new java.awt.Color(255, 0, 0));
        levelLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        levelLabel.setText("1");

        jLabel10.setFont(new java.awt.Font("微软雅黑", 2, 14)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(255, 255, 255));
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setText("LINES");

        linesLabel.setFont(new java.awt.Font("微软雅黑", 2, 14)); // NOI18N
        linesLabel.setForeground(new java.awt.Color(0, 0, 255));
        linesLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        linesLabel.setText("0");

        javax.swing.GroupLayout rightPanelLayout = new javax.swing.GroupLayout(rightPanel);
        rightPanel.setLayout(rightPanelLayout);
        rightPanelLayout.setHorizontalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(scoreLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(levelLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(rightPanelLayout.createSequentialGroup()
                        .addComponent(nextPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 6, Short.MAX_VALUE))
                    .addComponent(linesLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, rightPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(beginBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        rightPanelLayout.setVerticalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nextPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scoreLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(levelLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(linesLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
                .addComponent(beginBtn)
                .addContainerGap())
        );

        getContentPane().add(rightPanel, java.awt.BorderLayout.LINE_END);

        gamePanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        javax.swing.GroupLayout gamePanelLayout = new javax.swing.GroupLayout(gamePanel);
        gamePanel.setLayout(gamePanelLayout);
        gamePanelLayout.setHorizontalGroup(
            gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 178, Short.MAX_VALUE)
        );
        gamePanelLayout.setVerticalGroup(
            gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 358, Short.MAX_VALUE)
        );

        getContentPane().add(gamePanel, java.awt.BorderLayout.CENTER);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        //System.out.println(evt.getKeyCode());
        switch (evt.getKeyCode()) {
            case 37: {//左
                if (!this.paused) {
                    this.gamePanel.control.left(1);
                }
                break;
            }
            case 38: {//上-变形
                if (!this.paused) {
                    this.gamePanel.control.change();
                }
                break;
            }
            case 39: {//右
                if (!this.paused) {
                    this.gamePanel.control.right(1);
                }
                break;
            }
            case 32: {//空格-落下
                if (!this.paused) {
                    this.gamePanel.control.quickDown();
                }
                break;
            }
            case 40: {//下-下落
                if (!this.paused) {
                    this.gamePanel.control.down();
                }
                break;
            }
            case 10: {
            }
            //10
            case 96: {//换行-暂停  0
                if (this.paused) {
                    this.resume();
                } else {
                    this.pause();
                }
                break;
            }
            case 27: {//退出
                System.exit(0);
                break;
            }
        }
    }//GEN-LAST:event_formKeyPressed

    private void beginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_beginBtnActionPerformed

        this.requestFocus();
        this.beginBtn.setEnabled(false);
        this.start();
    }//GEN-LAST:event_beginBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainFrame mainFrame = new MainFrame();
                mainFrame.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton beginBtn;
    private ui.GamePanel gamePanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel4;
    public javax.swing.JLabel levelLabel;
    public javax.swing.JLabel linesLabel;
    private ui.NextPanel nextPanel;
    private javax.swing.JPanel rightPanel;
    public javax.swing.JLabel scoreLabel;
    // End of variables declaration//GEN-END:variables

    //开始游戏
    public void start() {
        if (this.gamePanel.getTimer() != null) {
            this.gamePanel.getTimer().cancel();
        }
        //初始化线程
        this.gamePanel.setTetrisTask(new TetrisTask(this.gamePanel));
        //初始化定时器
        this.gamePanel.setTimer(new Timer());
        this.paused = false;
        this.scoreLabel.setText(String.valueOf(this.gamePanel.getScore()));
    }

    //暂停游戏
    public void pause() {
        if (this.gamePanel.getTimer() != null) {
            this.gamePanel.getTimer().cancel();
        }
        this.gamePanel.setTimer(null);
        this.paused = true;
    }

    //恢复游戏
    public void resume() {
        if (!this.paused) {
            return;
        }
        //初始化线程
        this.gamePanel.setTetrisTask(new TetrisTask(this.gamePanel));
        //初始化定时器
        this.gamePanel.setTimer(new Timer());
        this.paused = false;
    }
}